package shark.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import shark.user.exception.SharkSystemException;
import shark.user.service.ISysUserService;
import shark.user.spi.entity.SysUser;
import shark.user.spi.entity.SysUserRole;
import shark.user.spi.mapper.SysUserMapper;
import shark.user.spi.mapper.SysUserRoleMapper;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class ISysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public boolean createUser(SysUser sysUser) {
        boolean result = this.save(sysUser);
        return result;
    }

    @Override
    @Transactional
    public void deleteById(String id) {
        Assert.hasText(id, "删除人员信息，id不能为空！");
        sysUserMapper.deleteById(id);
        sysUserRoleMapper.delete(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, id));
    }

    @Override
    public IPage<SysUser> getUsersByRoleId(Page<SysUser> page, String roleId) {
        Assert.hasText(roleId, "roleId不能为空！");
        return sysUserMapper.getUserByRoleId(page, roleId);
    }

    @Override
    public IPage<SysUser> getUsersByDeptId(Page<SysUser> page, String deptId) {
        Assert.hasText(deptId, "deptId不能为空！");
        return sysUserMapper.getUserByDeptId(page, deptId);
    }

    @Override
    public List<String> getRole(String username) {
        return sysUserRoleMapper.getRoleByUserName(username);
    }

    @Override
    public Set<String> getUserRolesSet(String username) {
        List<String> roles = sysUserRoleMapper.getRoleByUserName(username);
        log.info("-------通过数据库读取用户拥有的角色------username： " + username + ",Roles size: " + (roles == null ? 0 : roles.size()));
        return new HashSet<>(roles);
    }

    @Override
    public void resetPassword(String username, String oldPsw, String newPsw) {
        Assert.notNull(username, "用户重置密码，username不能为空！");
        Assert.notNull(oldPsw, "用户：" + username + "重置密码，旧密码不能为空！");
        Assert.notNull(newPsw, "用户：" + username + "重置密码，新密码不能为空！");

        SysUser user = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
        if (user == null) {
            throw new SharkSystemException("查询无此用户！用户名：" + username);
        }

        String encodeOldPsw = bCryptPasswordEncoder.encode(oldPsw);
        if (!encodeOldPsw.equals(user.getPassword())) {
            throw new SharkSystemException("修改密码，旧密码输入不正确！用户名：" + username);
        }

        user.setPassword(newPsw);
        sysUserMapper.update(user, new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
    }
}
